package mm.chenchen.modules.mh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import mm.chenchen.constant.ResultMsg;

import mm.chenchen.db.annotation.DataSource;
import mm.chenchen.modules.mh.entity.MhIncome;
import mm.chenchen.modules.mh.entity.model.res.LSRes;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import mm.chenchen.utils.R;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import mm.chenchen.modules.mh.entity.MhDecorationsInfo;
import mm.chenchen.modules.mh.dao.MhDecorationsInfoDao;
import mm.chenchen.utils.PageUtils;
import mm.chenchen.utils.PageModel;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import mm.chenchen.modules.mh.dao.model.request.MhDecorationsInfoQuery;
import org.springframework.util.ObjectUtils;


@Service("mhDecorationsInfoService")
@DataSource("mh_game_local")
public class MhDecorationsInfoServiceImpl extends ServiceImpl<MhDecorationsInfoDao, MhDecorationsInfo> {

    @Autowired
    private MhIncomeServiceImpl incomeService;
    public R findPage(Page<Map> page, MhDecorationsInfoQuery param) {
        IPage<MhDecorationsInfo> pageData = baseMapper.findPage(page, param);
        if (pageData != null) {
            pageData.getRecords().forEach(e -> {
                e.refreshDescription();
            });

            return R.ok().setData(new PageModel(pageData.getRecords(), pageData.getTotal(), pageData.getSize(), pageData.getCurrent()));
        } else {
            return R.error(ResultMsg.DATA_NOT_EXIT);
        } 
    }

    public R getById(Long id) {
        return R.ok().setData(baseMapper.findDetailById(id));
    }

    public R saveInfo(MhDecorationsInfo param) {
        incomeService.saveInfo(new MhIncome("o",param.getPrice().doubleValue(),param.getName()));
        return R.ok().setData(super.save(param));
    }

    public R update(MhDecorationsInfo param) {
        MhDecorationsInfo data = super.getById(param.getId());
        if (ObjectUtils.isEmpty(data)) {
            return R.error(ResultMsg.DATA_NOT_EXIT);
        }
        return R.ok().setData(super.updateById(param));
    }

    public R deleteById(Long id) {
        return R.ok().setData(super.removeById(id));
    }

    public R getNoUsedEQ(Long id) {
        MhDecorationsInfoQuery param=new MhDecorationsInfoQuery();
        param.setCurrentPage(1);
        param.setPageSize(1000);
        IPage<MhDecorationsInfo> pageData = baseMapper.findPage(PageUtils.getPage(param), param);
        List<MhDecorationsInfo> list = pageData.getRecords();
        list.forEach(e->{
            e.refreshDescription();
        });
        Map<Long, List<MhDecorationsInfo>> map = list.stream().collect(Collectors.groupingBy(MhDecorationsInfo::getType));
        LSRes res = new LSRes();
        res.setJieZhi(map.get(1l));
        res.setPeiShi(map.get(4l));
        res.setShouZhuo(map.get(3l));
        res.setErShi(map.get(2l));
        return R.ok().setData(res);
    }
}